home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / AppInstall / PackageWorker.py < prev    next >
Encoding:
Python Source  |  2009-03-31  |  3.7 KB  |  113 lines

  1. # (c) 2005-2007 Canonical, GPL
  2.  
  3. import apt_pkg
  4. import subprocess
  5. import gtk
  6. import gtk.gdk
  7. import thread
  8. import time
  9. import os
  10. import tempfile
  11. from gettext import gettext as _
  12.  
  13. class PackageWorker:
  14.     """
  15.     A class which does the actual package installing/removing.
  16.     """
  17.  
  18.     # synaptic actions
  19.     (INSTALL, UPDATE) = range(2)
  20.  
  21.     def __init__(self, addon_cd=None):
  22.         self.addon_cd=addon_cd
  23.     
  24.     def run_synaptic(self, id, lock, to_add=None,to_rm=None, action=INSTALL):
  25.         #apt_pkg.PkgSystemUnLock()
  26.         #print "run_synaptic(%s,%s,%s)" % (id, lock, selections)
  27.         cmd = []
  28.         if os.getuid() != 0:
  29.             cmd = ["/usr/bin/gksu",
  30.                    "--desktop", "/usr/share/applications/synaptic.desktop",
  31.                    "--"]
  32.         cmd += ["/usr/sbin/synaptic",
  33.                 "--hide-main-window",
  34.                 "--non-interactive",
  35.                 "-o", "Synaptic::closeZvt=true",
  36.                 "--parent-window-id", "%s" % (id) ]
  37.  
  38.         # create tempfile for install (here because it must survive
  39.         # durng the synaptic call
  40.         f = tempfile.NamedTemporaryFile()
  41.         if action == self.INSTALL:
  42.             # setup the cdrom 
  43.             if self.addon_cd:
  44.                 cmd += ["-o","Acquire::cdrom::mount=%s" % self.addon_cd]
  45.             # install the stuff
  46.             for item in to_add:
  47.                 f.write("%s\tinstall\n" % item)
  48.                 #print item.pkgname
  49.             for item in to_rm:
  50.                 f.write("%s\tuninstall\n" % item)
  51.             cmd.append("--set-selections-file")
  52.             cmd.append("%s" % f.name)
  53.             f.flush()
  54.         elif action == self.UPDATE:
  55.             #print "Updating..."
  56.             cmd.append("--update-at-startup")
  57.         self.return_code = subprocess.call(cmd)
  58.         lock.release()
  59.         f.close()
  60.  
  61.     def plug_removed(self, w, (win,socket)):
  62.         # plug was removed, but we don't want to get it removed, only hiden
  63.         # unti we get more
  64.         win.hide()
  65.         return True
  66.  
  67.     def plug_added(self, sock, win):
  68.         while gtk.events_pending():
  69.             win.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
  70.             win.show()
  71.             #print "huhu"
  72.             gtk.main_iteration()
  73.  
  74.     def get_plugged_win(self, window_main):
  75.         win = gtk.Window()
  76.         win.realize()
  77.         win.window.set_functions(gtk.gdk.FUNC_MOVE)
  78.         win.set_border_width(6)
  79.         win.set_transient_for(window_main)
  80.         win.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
  81.         win.set_title("")
  82.         win.set_resizable(False)
  83.         win.set_property("skip-taskbar-hint", True)
  84.         win.set_property("skip-taskbar-hint", True)
  85.         # prevent the window from closing with the delete button (there is
  86.         # a cancel button in the window)
  87.         win.connect("delete_event", lambda e,w: True);
  88.     
  89.         # create the socket
  90.         socket = gtk.Socket()
  91.         socket.show()
  92.         win.add(socket)
  93.         
  94.         socket.connect("plug-added", self.plug_added, win)
  95.         socket.connect("plug-removed", self.plug_removed, (win,socket))
  96.  
  97.         
  98.         return win, socket
  99.     
  100.     def perform_action(self, window_main, cache, to_add=None, to_rm=None, action=INSTALL):
  101.         window_main.set_sensitive(False)
  102.         window_main.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.WATCH))
  103.         lock = thread.allocate_lock()
  104.         lock.acquire()
  105.         t = thread.start_new_thread(self.run_synaptic,(window_main.window.xid,lock,to_add, to_rm, action))
  106.         while lock.locked():
  107.             while gtk.events_pending():
  108.                 gtk.main_iteration()
  109.             time.sleep(0.05)
  110.         window_main.set_sensitive(True)
  111.         window_main.window.set_cursor(None)
  112.         return self.return_code
  113.